<?php
namespace App\Http\Controllers\Admin;

use App\Model\ShopMaster;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Config;
use Log;
use SplFileObject;
use App\Model\CorpMaster;

class ShopController extends Controller
{

    function queryShopMaster(Request $request)
    {
        $auth = cookie::get('auth');
        if ($auth == Config::get('const.adrojapan')) {
            $corpId = cookie::get('sel_corp_id');
        } else {
            $corpId = cookie::get('corp_id');
        }

        $corpMaster = new CorpMaster();

        $corpInfo = $corpMaster->getCorpNameCorpMaster($corpId);
        $shopMaster = new ShopMaster();
        $shopInfo = $shopMaster->getShopMaster();

        $corpIdarr = array(
            $corpId
        );

        $allInfo = [
            $shopInfo,
            $corpInfo,
            $corpIdarr
        ];

        return view('admin.shoplist', array(
            'allInfo' => $allInfo
        ));

        $shopInfoPara = Null;

        if (! empty($shopId)) {
            $shopMaster = new ShopMaster();
            $shopInfo = $shopMaster->getShopInfoByLoginId($shopId);
            if (! empty($shopInfo[0])) {
                $shopInfoPara = $shopInfo[0];
            }
        }
        $allInfo = [
            $shopInfoPara,
            $corpInfo
        ];

        return view('admin.shoplist', array(
            'allInfo' => $allInfo
        )); // 返回视图
    }

    function index(Request $request)
    {
        $optioninfo = $request->input('optioninfo');
        $corpName = $request->input('corp_name');
        $corpId = $request->input('corp_id');

        if (empty($optioninfo)) {
            $shopId = cookie::get('shop_id');
        } else if ($optioninfo == 'create') {
            $shopId = $request->input('shop_id');
        }

        $login_id = cookie::get('login_id');
        $user_id = cookie::get('user_id');
        $password = cookie::get('password');

        $userInfo = array(
            $login_id,
            $user_id,
            $password,
            $corpId,
            $corpName
        );

        $shopInfoPara = Null;

        if (! empty($shopId)) {
            $shopMaster = new ShopMaster();
            $shopInfo = $shopMaster->getShopInfoByLoginId($shopId);
            if (! empty($shopInfo[0])) {
                $shopInfoPara = $shopInfo[0];
            }
        } else {
            $shopInfoPara = new ShopMaster();
        }

        $allInfo = [
            $shopInfoPara,
            $userInfo
            // $shopInfoCorp
        ];
        $corpMaster = new CorpMaster();
        $corpList = $corpMaster->getCorpMaster();

        return view('admin.shop_mst', array(
            'allInfo' => $allInfo,
            'corpList' => $corpList
        )); // 返回视图
    }

    public function addShopMaster(Request $request)
    {
        $shopMaster = new ShopMaster();
        $shopInfo = $request->input('ShopMaster');
        $shop_id = $shopInfo['shop_id'];

        if (empty($shop_id)) {
            $shopInfo['created_at'] = now();
            $shopInfo['updated_at'] = now();
            $shopInfo['delete_flg'] = 0;

            $shop_id = $shopMaster->saveShopMaster($shopInfo);

            if (! empty($shop_id)) {
                // return view('/admin/shoplist')->with('success', '新規成功！');
                return redirect('/admin/shoplist')->with('success', '新規成功！');
            } else {
                return view('/admin/shoplist')->with('error', '新規失敗！');
            }
        } else {

            $list = 'shop_id';
            $data = $shop_id;

            $shopInfo['updated_at'] = now();
            $flag = $shopMaster->updShopMaster($data, $list, $shopInfo);
            if ($flag) {
                return redirect('/admin/shoplist')->with('success', '修正成功！');
            } else {
                return redirect('/admin/shoplist')->with('success', '修正失敗！');
            }
        }
    }

    public function updShopMaster(Request $request)
    {
        $shopMaster = $request->input('ShopMaster');
        $shop_id = $shopMaster;
        $flag = ShopMaster::updShopMaster($shopMaster);
        // return $news;//直接返回json
        return redirect('/admin/shop_mst')->with('success', '操作成功！');
    }

    public function delShopMaster(Request $request)
    {
        $shopMasterInfo = $request->input('ShopMaster');
        $shop_id = $shopMasterInfo['shop_id'];

        $shopMaster = new ShopMaster();
        $flag = $shopMaster->delShopMaster($shop_id);

        if ($flag) {
            return redirect('/admin/shoplist')->with('success', '削除成功！');
        } else {
            return redirect('/admin/shoplist')->with('error', '削除失敗！');
        }
    }

    public function delshopInfo(Request $request)
    {
        $shop_id = $request->input('shop_id');

        $shopMaster = new ShopMaster();
        $flag = $shopMaster->delShopMaster($shop_id);

        if ($flag) {
            return redirect('/admin/shoplist')->with('success', '削除成功！');
        } else {
            return redirect('/admin/shoplist')->with('error', '削除失敗！');
        }
    }

    public function inputCsvShopMaster(Request $request)
    {
        try {
            // setlocaleを設定
            setlocale(LC_ALL, 'ja_JP.UTF-8');

            // アップロードしたファイルを取得
            $uploaded_file = $request->file('csv_file');
            $filecsv = $_FILES['csv_file'];
            // ファイル空の判断
            // ファイルフォーマットの判断
            $file_type = substr(strstr($filecsv['name'], '.'), 1);
            if ($file_type != 'csv') {
                echo "<script type=\"text/javascript\">alert(\"ファイルフォーマットのエラーです,もう一度アップロードしてください!\");window.history.go(-1);</script>";
                exit();
            }

            // アップロードしたファイルの絶対パスを取得
            $file_path = $request->file('csv_file')->path($uploaded_file);
            $file = new SplFileObject($file_path);
            $file->setFlags(SplFileObject::READ_CSV);

            $row_count = 1;
            $flag = false;
            foreach ($file as $row) {

                if (isset($row[0]) && ! empty($row)) {

                    // 1行目のヘッダーは取り込まない
                    if ($row_count > 1) {
                        if (isset($row[0]) && ! empty($row[0])) {
                            $shop_name = mb_convert_encoding($row[0], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[1]) && ! empty($row[1])) {
                            // $shop_name = mb_convert_encoding($row[1], 'UTF-8', 'SJIS');
                            $corp_id = mb_convert_encoding($row[1], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[2]) && ! empty($row[2])) {
                            $shop_kana = mb_convert_encoding($row[2], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[3]) && ! empty($row[3])) {
                            $shop_postcode = mb_convert_encoding($row[3], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[4]) && ! empty($row[4])) {
                            $shop_adress = mb_convert_encoding($row[4], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[5]) && ! empty($row[5])) {
                            $shop_biru_name = mb_convert_encoding($row[5], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[6]) && ! empty($row[6])) {
                            $shop_telephone_number = mb_convert_encoding($row[6], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[7]) && ! empty($row[7])) {
                            $shop_fax_number = mb_convert_encoding($row[7], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[8]) && ! empty($row[8])) {
                            $stuff_name = mb_convert_encoding($row[8], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[9]) && ! empty($row[9])) {
                            $stuff_kana = mb_convert_encoding($row[9], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[10]) && ! empty($row[10])) {
                            $shop_mail = mb_convert_encoding($row[10], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[11]) && ! empty($row[11])) {
                            $shop_tel = mb_convert_encoding($row[11], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[12]) && ! empty($row[12])) {
                            $delete_flg = mb_convert_encoding($row[12], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[13]) && ! empty($row[13])) {
                            $created_at = mb_convert_encoding($row[13], 'UTF-8', 'SJIS');
                        }
                        if (isset($row[14]) && ! empty($row[14])) {
                            $updated_at = mb_convert_encoding($row[14], 'UTF-8', 'SJIS');
                        }
                    }
                    // ここで値をデータベースに保存したりする
                    $shopMaster = new ShopMaster();
                    $flag = $shopMaster->addCsvShopMasterr($row);
                }
                $row_count ++;
            }

            if ($flag) {
                return redirect('/admin/shoplist')->with('success', '取込成功！');
            } else {
                return redirect('/admin/shoplist')->with('error', '取込失敗！');
            }
        } catch (\Exception $e) {
            \Log::error($e->getFile() . " Line:" . $e->getLine() . " Msg:" . $e->getMessage());
            return redirect('/admin/shoplist')->with('success', '取込成功！');
        }
    }
}

